<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from'@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				userCacheKey:'hr-u-tlApproval-user-cache',
				busCode:""
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				if(_that.edit){
					_that.om.formSave("tlApproval/addTlApproval",_that.formdata,_that)
				}else{
					_that.om.request(
						"posBusiAuth/verifyOne",
						{
							busCode:_that.busCode,
							isNew:true
						}
					).then(res=>{
						if(res.succeed){
							_that.om.formSave("tlApproval/addTlApproval",_that.formdata,_that)
						}
					})
				}
			},
			onSelectAttEndDate(item){
				let _that = this
				if(_that.$refs.autoform.formdata.effectDate){
					let attendDate = moment(item).format('YYYY-MM-DD')
					let effectDate = moment(_that.$refs.autoform.formdata.effectDate).format('YYYY-MM-DD')
					let editor1 = _that.$refs.autoform.getEditor("leaveInfo","effectDate")
					let editor2 = _that.$refs.autoform.getEditor("attendanceInfo","attEndDate")
					if(effectDate !== attendDate){
						confirmModal(_that,"提示信息","“"+editor1.title+"”和"+"“"+editor2.title+"”不在同一天，是否继续选择？",function(){},function(){
							_that.$refs.autoform.clear("attendanceInfo","attEndDate")
						})
					}
				}
			},
			onSelectEffectDate(item){
				let _that = this
				let effectDate = moment(item).format('YYYY-MM-DD')
				let attendDate = moment(_that.$refs.autoform.formdata.attEndDate).format('YYYY-MM-DD')
				let editor1 = _that.$refs.autoform.getEditor("leaveInfo","effectDate")
				let editor2 = _that.$refs.autoform.getEditor("attendanceInfo","attEndDate")
				let date = new Date()
				let today = moment(date,'YYYY-MM-DD')
				if(_that.$refs.autoform.formdata.attEndDate){
					if(effectDate !== attendDate){
							confirmModal(_that,"提示信息","“"+editor1.title+"”和"+"“"+editor2.title+"”不在同一天，是否继续选择？",function(){},function(){
							_that.$refs.autoform.clear("leaveInfo","effectDate")
						})					
					}
				}
				if(_that.$refs.autoform.formdata.leaveTypeCode ==="37732"){
					let diffDay = moment(today).isBefore(effectDate, 'year');
					if(!diffDay){
						_that.om.toast(_that.$refs.autoform.formdata.leaveTypeName+"需要小于当期日期！")
					}
				}
			},
			getContractInfo(data){
				let _that = this
				_that.om.request(
					"employContractList/getContractInfo",
					{
						userId:_that.$refs.autoform.formdata.infonameId
					}
				).then(res=>{
					if(res.value && res.value.contractEndDate){
						_that.$refs.autoform.formdata.laborTypeCode = data[0].code
						_that.$refs.autoform.formdata.laborTypeName = data[0].dicname
						_that.$refs.autoform.formdata.laborEnd = res.value.contractEndDate
						_that.$refs.autoform.initSelectIndex("laborRelateInfo","laborTypeCode")
					}
				})
			},
			getLaborTypeList(){
				let _that = this
				_that.om.request(
					"dictZero/getListByCode",
					{
						code:37689
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor("laborRelateInfo","laborTypeCode","type","selectbox")
					_that.$refs.autoform.changeEditor("laborRelateInfo","laborTypeCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.changeEditor("laborRelateInfo","laborEnd","type","datebox")
					_that.$refs.autoform.changeEditor("laborRelateInfo","laborEnd","prompt","请选择")
					_that.getContractInfo(res)
				})
			},
			getLeaveTypeList(){
				let _that = this
				_that.om.request(
					"tlApproval/getLeaveType",
					{
						userId:_that.$refs.autoform.formdata.infonameId
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor("leaveInfo","leaveTypeCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.initSelectIndex("leaveInfo","leaveTypeCode")
				})
			},
			onSelectLaborState(item){
				let _that = this
				if(item.code !=="20063"&&item.code!=="20067"){
					_that.getLaborTypeList()
				}else{
					_that.$refs.autoform.changeEditor("laborRelateInfo","laborTypeCode","type","labelbox")
					_that.$refs.autoform.clear("laborRelateInfo","laborTypeCode")
					_that.$refs.autoform.changeEditor("laborRelateInfo","laborEnd","type","labelbox")
					_that.$refs.autoform.clear("laborRelateInfo","laborEnd")
				}
			},
			getLaborStateList(){
				let _that = this
				_that.om.request(
					"dictZero/getListByCode",
					{
						code:"20012"
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor("laborRelateInfo","laborStateCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.initSelectIndex("laborRelateInfo",'laborStateCode')
				})
			},
			getEmpBasicInfo(id){
				let _that = this
				_that.om.request(
					"temployee/getEmpBasicInfo",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.sexName = res.value.sexName
					_that.$refs.autoform.formdata.birthdate =  res.value.birthdate
					_that.$refs.autoform.formdata.birthPlace =  res.value.birthPlace
					_that.$refs.autoform.formdata.highestEduName=  res.value.highestEduName
					_that.$refs.autoform.formdata.entrydate=  res.value.entrydate
					_that.$refs.autoform.formdata.mobile=  res.value.mobile
					_that.$refs.autoform.formdata.areaName=  res.value.areaName
					_that.$refs.autoform.formdata.idTypeName= res.value.idTypeName
					_that.$refs.autoform.formdata.idcard= res.value.idcard
					_that.$refs.autoform.formdata.certificateValidDate= res.value.certificateValidDate
					_that.$refs.autoform.formdata.nationName= res.value.nationName
					_that.$refs.autoform.formdata.marriageName= res.value.marriageName
					_that.$refs.autoform.formdata.politicalName= res.value.politicalName
					_that.$refs.autoform.formdata.bloodTypeName= res.value.bloodTypeName
					_that.$refs.autoform.formdata.userno= res.value.userno
					_that.$refs.autoform.formdata.registerCategoryName= res.value.registerCategoryName
					_that.$refs.autoform.formdata.birthaddress= res.value.birthaddress
					_that.$refs.autoform.formdata.currentaddress= res.value.currentaddress
				})
			},
			getInfo1(userId){
				let _that = this
				_that.om.request(
					"tworkInfo/getInfo1",
					{
						userId:userId
					}
				).then(res=>{
					if(res.value){
						_that.subEditorObj["workInfo"].workList = res.value
					}
				})
			},
			verifyUser(userId){
				let _that = this
				_that.om.request(
					"tlApproval/verifySame",
					{
						userId:userId
					},
					"post"
				).then(res=>{
					if(res.value){
						for(let index in res.value){
							if(index === "one"){
								errorModal(_that,"提示信息",ignoreHtml(res.value[index]),function(){
									_that.$refs.autoform.clearGroup("userInfo")
									_that.subEditorObj["workInfo"].workList = []
								})
							}else if(index === "two"){
								confirmModal(_that,"提示信息",ignoreHtml(res.value[index]),function(){},function(){
									_that.$refs.autoform.clearGroup("userInfo")
									_that.subEditorObj["workInfo"].workList = []
								})
							}else if(index === "t"){
								_that.$refs.autoform.formdata.leaveDate = res.value.t.leaveDate
							}
						}
					}
				})
			},
			getUserInfo(userId){
				let _that = this
				_that.om.request(
					"tuserInfo/getInfo",
					{
						userId:userId
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.formdata.infoeducation = res.value.eduId
					_that.$refs.autoform.formdata.edu = res.value.edu
				})
			},
			initLoad(options){
				let _that = this
				_that.busCode = options.code
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"tlApproval/getInfo",
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("userInfo","infonameId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({}),
							namefield:urlUtil.findEmployee().namefield,
							notefield:urlUtil.findEmployee().notefield,
							extrafield:urlUtil.findEmployee().extrafield,
							url:urlUtil.findEmployee().url,
							cachename:_that.userCacheKey,
							searchfield:"q",
							initload:true
						}
					},null,mainData)
					if(_that.subEditorObj["workInfo"]){
						changeEditor("workInfo","workStr",{
							showAdd:false,
							showDel:false,
							showArrow:false,
							titleTemplate:"岗位名称:<%=posName || '未填写'%>|上岗日期:<%=workDate || '未填写'%>",
							subtitle1Template:"任职方式:<%=workWayName || '未填写'%>|机构简称:<%=instShort || '未填写'%>",
							subtitle2Template:"上级机构:<%=parentInst || '未填写'%>|岗位职级:<%=posCareercat || '未填写'%>",
							subtitle3Template:"职务级别:<%=posLevel || '未填写'%>|计酬凡是:<%=salaryWay || '未填写'%>"
						},null,mainData)
					}
					if(_that.subEditorObj["handleInfo"]){
						changeEditor("handleInfo","objStr",{
							titleTemplate:"交接项目名称:<%=itemName || '未填写'%>",
							subtitle1Template:"经办岗位:<%=handlePosName || '未填写'%>",
							subtitle2Template:"经办人:<%=handleUserName || '未填写'%>",
						},null,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id","code")
						for(let index in res[1].value.tlApproval){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.tlApproval[index])
								_that.$set(_that.formdata,index,res[1].value.tlApproval[index])
							}
						}
						for(let field in res[1].value.tuserInfo){
							if(fieldArr.includes(field)){
								if(field !== "id"){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,field,res[1].value.tuserInfo[field])
									_that.$set(_that.formdata,field,res[1].value.tuserInfo[field])
								}
							}
						}
						let workInfo = _that._.cloneDeep(res[1].value.tworkInfos)
						_that.subEditorObj['workInfo'].workList = workInfo
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'workStr',JSON.stringify(workInfo))
						_that.$set(_that.formdata,"workStr",JSON.stringify(workInfo))
						
						let handleInfo = _that._.cloneDeep(res[1].value.tlApproval_handles)
						_that.subEditorObj['handleInfo'].workList = handleInfo
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'objStr',JSON.stringify(handleInfo))
						_that.$set(_that.formdata,"objStr",JSON.stringify(handleInfo))
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							_that.getLeaveTypeList(_that.$refs.autoform.formdata.infonameId)
							_that.getLaborStateList()
						})
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
					//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.infonameId = d.id
							_that.$refs.autoform.clearGroup("workInfo")
							_that.$refs.autoform.clearGroup("laborRelateInfo")
							_that.$refs.autoform.clearGroup("leaveReasonInfo")
							_that.$refs.autoform.clearGroup("leaveInfo")
							_that.$refs.autoform.clearGroup("handleInfo")
							_that.$refs.autoform.clearGroup("remarkInfo")
							_that.verifyUser(d.id)
							_that.getInfo1(d.id)
							_that.getLeaveTypeList(d.id)
							formdata.userName = d.username
							_that.getEmpBasicInfo(d.id)
							_that.getUserInfo(d.id)
							_that.getLaborStateList()
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style></style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
